package cn.qdhlm.collectservice.core.component.command;

import cn.qdhlm.collectservice.common.Constant;
import cn.qdhlm.collectservice.common.util.CodeUtil;
import cn.qdhlm.collectservice.common.util.ComUtil;
import cn.qdhlm.collectservice.common.util.JsonUtil;
import cn.qdhlm.collectservice.model.dto.CommandParamDTO;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.extern.log4j.Log4j2;

import java.math.BigDecimal;

/**
 * @Name: CmdAfn13
 * @Description: 阀门控制（AFN=13H）
 * @Author: jiangcs
 */
@Log4j2
public class CmdAfn13 {
    /**
     * 开阀门
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f2(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getTerminalNo())) {
            log.error("f2 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(20);
        // DT=> F2
        byteBuf.writeShort(0x0200);
        // SN
        byteBuf.writeShort(sn);
        // terminalNo
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getTerminalNo())));
        // 不够16位，填充 0x00
        int needFillZeroCount = Constant.BYTE_LENGTH_TERMINAL_NO - commandParamDTO.getTerminalNo().length();
        if (needFillZeroCount > 0) {
            for (int i = 0; i < needFillZeroCount; i++) {
                byteBuf.writeByte(0x00);
            }
        }
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 关阀门
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f3(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getTerminalNo())) {
            log.error("f3 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(20);
        // DT=> F3
        byteBuf.writeShort(0x0400);
        // SN
        byteBuf.writeShort(sn);
        // terminalNo
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getTerminalNo())));
        // 不够16位，填充 0x00
        int needFillZeroCount = Constant.BYTE_LENGTH_TERMINAL_NO - commandParamDTO.getTerminalNo().length();
        if (needFillZeroCount > 0) {
            for (int i = 0; i < needFillZeroCount; i++) {
                byteBuf.writeByte(0x00);
            }
        }
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 强制开阀
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f4(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getTerminalNo())) {
            log.error("f4 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(20);
        // DT=> F4
        byteBuf.writeShort(0x0800);
        // SN
        byteBuf.writeShort(sn);
        // terminalNo
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getTerminalNo())));
        // 不够16位，填充 0x00
        int needFillZeroCount = Constant.BYTE_LENGTH_TERMINAL_NO - commandParamDTO.getTerminalNo().length();
        if (needFillZeroCount > 0) {
            for (int i = 0; i < needFillZeroCount; i++) {
                byteBuf.writeByte(0x00);
            }
        }
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 强制关阀
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f5(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getTerminalNo())) {
            log.error("f5 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(20);
        // DT=> F5
        byteBuf.writeShort(0x1000);
        // SN
        byteBuf.writeShort(sn);
        // terminalNo
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getTerminalNo())));
        // 不够16位，填充 0x00
        int needFillZeroCount = Constant.BYTE_LENGTH_TERMINAL_NO - commandParamDTO.getTerminalNo().length();
        if (needFillZeroCount > 0) {
            for (int i = 0; i < needFillZeroCount; i++) {
                byteBuf.writeByte(0x00);
            }
        }
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 解锁阀门
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f6(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getTerminalNo())) {
            log.error("f6 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(20);
        // DT=> F6
        byteBuf.writeShort(0x2000);
        // SN
        byteBuf.writeShort(sn);
        // terminalNo
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getTerminalNo())));
        // 不够16位，填充 0x00
        int needFillZeroCount = Constant.BYTE_LENGTH_TERMINAL_NO - commandParamDTO.getTerminalNo().length();
        if (needFillZeroCount > 0) {
            for (int i = 0; i < needFillZeroCount; i++) {
                byteBuf.writeByte(0x00);
            }
        }
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 强制设温
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f7(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getTerminalNo()) || ComUtil.isEmpty(commandParamDTO.getTemperatureRoom())) {
            log.error("f7 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(22);
        // DT=> F7
        byteBuf.writeShort(0x4000);
        // SN
        byteBuf.writeShort(sn);
        // terminalNo
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getTerminalNo())));
        // 不够16位，填充 0x00
        int needFillZeroCount = Constant.BYTE_LENGTH_TERMINAL_NO - commandParamDTO.getTerminalNo().length();
        if (needFillZeroCount > 0) {
            for (int i = 0; i < needFillZeroCount; i++) {
                byteBuf.writeByte(0x00);
            }
        }
        // 设定温度
        byteBuf.writeShortLE(BigDecimal.valueOf(commandParamDTO.getTemperatureRoom()).multiply(BigDecimal.valueOf(100)).shortValue());
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 解锁设温
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f8(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getTerminalNo()) || ComUtil.isEmpty(commandParamDTO.getWorkMode())) {
            log.error("f8 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(21);
        // DT=> F8
        byteBuf.writeShort(0x8000);
        // SN
        byteBuf.writeShort(sn);
        // terminalNo
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getTerminalNo())));
        // 不够16位，填充 0x00
        int needFillZeroCount = Constant.BYTE_LENGTH_TERMINAL_NO - commandParamDTO.getTerminalNo().length();
        if (needFillZeroCount > 0) {
            for (int i = 0; i < needFillZeroCount; i++) {
                byteBuf.writeByte(0x00);
            }
        }
        // 工作模式
        byteBuf.writeByte(commandParamDTO.getWorkMode());
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 广播设温
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f11(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getTemperatureSet())) {
            log.error("f11 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(6);
        // DT=> F11
        byteBuf.writeShort(0x0401);
        // SN
        byteBuf.writeShort(sn);
        // 设定温度
        byteBuf.writeShort(BigDecimal.valueOf(commandParamDTO.getTemperatureSet()).multiply(BigDecimal.valueOf(100)).shortValue());
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 广播解锁设温
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f15(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getWorkMode())) {
            log.error("f15 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(4);
        // DT=> F15
        byteBuf.writeShort(0x4001);
        // SN
        byteBuf.writeShort(sn);
        // 工作模式
        byteBuf.writeByte(commandParamDTO.getWorkMode());
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 广播强开
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f18(int sn, CommandParamDTO commandParamDTO) {
        ByteBuf byteBuf = Unpooled.buffer(4);
        // DT=> F18
        byteBuf.writeShort(0x0202);
        // SN
        byteBuf.writeShort(sn);
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 广播强关
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f19(int sn, CommandParamDTO commandParamDTO) {
        ByteBuf byteBuf = Unpooled.buffer(4);
        // DT=> F19
        byteBuf.writeShort(0x0402);
        // SN
        byteBuf.writeShort(sn);
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 广播解锁阀门
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f20(int sn, CommandParamDTO commandParamDTO) {
        ByteBuf byteBuf = Unpooled.buffer(4);
        // DT=> F20
        byteBuf.writeShort(0x0802);
        // SN
        byteBuf.writeShort(sn);
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 开度设置
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f26(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getTerminalNo()) || ComUtil.isEmpty(commandParamDTO.getOpenDegree())) {
            log.error("f26 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(21);
        // DT=> F26
        byteBuf.writeShort(0x0203);
        // SN
        byteBuf.writeShort(sn);
        // terminalNo
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getTerminalNo())));
        // 不够16位，填充 0x00
        int needFillZeroCount = Constant.BYTE_LENGTH_TERMINAL_NO - commandParamDTO.getTerminalNo().length();
        if (needFillZeroCount > 0) {
            for (int i = 0; i < needFillZeroCount; i++) {
                byteBuf.writeByte(0x00);
            }
        }
        // 开度
        byteBuf.writeByte(commandParamDTO.getOpenDegree());
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 开度范围设置
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f27(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getTerminalNo()) || ComUtil.isEmpty(commandParamDTO.getOpenDegreeUpper()) || ComUtil.isEmpty(commandParamDTO.getOpenDegreeLower())) {
            log.error("f27 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(22);
        // DT=> F27
        byteBuf.writeShort(0x0403);
        // SN
        byteBuf.writeShort(sn);
        // terminalNo
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getTerminalNo())));
        // 不够16位，填充 0x00
        int needFillZeroCount = Constant.BYTE_LENGTH_TERMINAL_NO - commandParamDTO.getTerminalNo().length();
        if (needFillZeroCount > 0) {
            for (int i = 0; i < needFillZeroCount; i++) {
                byteBuf.writeByte(0x00);
            }
        }
        // 上限
        byteBuf.writeByte(commandParamDTO.getOpenDegreeUpper());
        // 下限
        byteBuf.writeByte(commandParamDTO.getOpenDegreeLower());
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 开度上限设置
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f28(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getTerminalNo()) || ComUtil.isEmpty(commandParamDTO.getOpenDegreeUpper())) {
            log.error("f28 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(21);
        // DT=> F28
        byteBuf.writeShort(0x0803);
        // SN
        byteBuf.writeShort(sn);
        // terminalNo
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getTerminalNo())));
        // 不够16位，填充 0x00
        int needFillZeroCount = Constant.BYTE_LENGTH_TERMINAL_NO - commandParamDTO.getTerminalNo().length();
        if (needFillZeroCount > 0) {
            for (int i = 0; i < needFillZeroCount; i++) {
                byteBuf.writeByte(0x00);
            }
        }
        // 上限
        byteBuf.writeByte(commandParamDTO.getOpenDegreeUpper());
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 取消远程模式
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f29(int sn, CommandParamDTO commandParamDTO) {
        ByteBuf byteBuf = Unpooled.buffer(20);
        // DT=> F29
        byteBuf.writeShort(0x1003);
        // SN
        byteBuf.writeShort(sn);
        // terminalNo
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getTerminalNo())));
        // 不够16位，填充 0x00
        int needFillZeroCount = Constant.BYTE_LENGTH_TERMINAL_NO - commandParamDTO.getTerminalNo().length();
        if (needFillZeroCount > 0) {
            for (int i = 0; i < needFillZeroCount; i++) {
                byteBuf.writeByte(0x00);
            }
        }
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 温度范围设置
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f30(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getTerminalNo()) || ComUtil.isEmpty(commandParamDTO.getTemperatureRoomUpper()) || ComUtil.isEmpty(commandParamDTO.getTemperatureRoomLower())) {
            log.error("f30 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(24);
        // DT=> F30
        byteBuf.writeShort(0x2003);
        // SN
        byteBuf.writeShort(sn);
        // terminalNo
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getTerminalNo())));
        // 不够16位，填充 0x00
        int needFillZeroCount = Constant.BYTE_LENGTH_TERMINAL_NO - commandParamDTO.getTerminalNo().length();
        if (needFillZeroCount > 0) {
            for (int i = 0; i < needFillZeroCount; i++) {
                byteBuf.writeByte(0x00);
            }
        }
        // 上限
        byteBuf.writeShortLE(BigDecimal.valueOf(commandParamDTO.getTemperatureRoomUpper()).multiply(BigDecimal.valueOf(100)).shortValue());
        // 下限
        byteBuf.writeShortLE(BigDecimal.valueOf(commandParamDTO.getTemperatureRoomLower()).multiply(BigDecimal.valueOf(100)).shortValue());
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 面板控制阀门
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f31(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getTerminalNo())) {
            log.error("f31 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(20);
        // DT=> F32
        byteBuf.writeShort(0x4003);
        // SN
        byteBuf.writeShort(sn);
        // terminalNo
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getTerminalNo())));
        // 不够16位，填充 0x00
        int needFillZeroCount = Constant.BYTE_LENGTH_TERMINAL_NO - commandParamDTO.getTerminalNo().length();
        if (needFillZeroCount > 0) {
            for (int i = 0; i < needFillZeroCount; i++) {
                byteBuf.writeByte(0x00);
            }
        }
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 远程控制开度
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f32(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getTerminalNo()) || ComUtil.isEmpty(commandParamDTO.getOpenDegree())) {
            log.error("f32 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(21);
        // DT=> F32
        byteBuf.writeShort(0x8003);
        // SN
        byteBuf.writeShort(sn);
        // terminalNo
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getTerminalNo())));
        // 不够16位，填充 0x00
        int needFillZeroCount = Constant.BYTE_LENGTH_TERMINAL_NO - commandParamDTO.getTerminalNo().length();
        if (needFillZeroCount > 0) {
            for (int i = 0; i < needFillZeroCount; i++) {
                byteBuf.writeByte(0x00);
            }
        }
        // 开度
        byteBuf.writeByte(commandParamDTO.getOpenDegree());
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 回温自动控制开度
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f33(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getTerminalNo()) || ComUtil.isEmpty(commandParamDTO.getTemperatureOut())) {
            log.error("f33 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(22);
        // DT=> F33
        byteBuf.writeShort(0x0104);
        // SN
        byteBuf.writeShort(sn);
        // terminalNo
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getTerminalNo())));
        // 不够16位，填充 0x00
        int needFillZeroCount = Constant.BYTE_LENGTH_TERMINAL_NO - commandParamDTO.getTerminalNo().length();
        if (needFillZeroCount > 0) {
            for (int i = 0; i < needFillZeroCount; i++) {
                byteBuf.writeByte(0x00);
            }
        }
        // 回水目标温度
        byteBuf.writeShortLE(BigDecimal.valueOf(commandParamDTO.getTemperatureOut()).multiply(BigDecimal.valueOf(100)).shortValue());
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 温差自动控制开度
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f34(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getTerminalNo()) || ComUtil.isEmpty(commandParamDTO.getTemperatureDiff())) {
            log.error("f34 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(22);
        // DT=> F34
        byteBuf.writeShort(0x0204);
        // SN
        byteBuf.writeShort(sn);
        // terminalNo
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getTerminalNo())));
        // 不够16位，填充 0x00
        int needFillZeroCount = Constant.BYTE_LENGTH_TERMINAL_NO - commandParamDTO.getTerminalNo().length();
        if (needFillZeroCount > 0) {
            for (int i = 0; i < needFillZeroCount; i++) {
                byteBuf.writeByte(0x00);
            }
        }
        // 温差目标温度
        byteBuf.writeShortLE(BigDecimal.valueOf(commandParamDTO.getTemperatureDiff()).multiply(BigDecimal.valueOf(100)).shortValue());
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 启用自动调平功能
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f35(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getTerminalNo())) {
            log.error("f35 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(20);
        // DT=> F35
        byteBuf.writeShort(0x0404);
        // SN
        byteBuf.writeShort(sn);
        // terminalNo
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getTerminalNo())));
        // 不够16位，填充 0x00
        int needFillZeroCount = Constant.BYTE_LENGTH_TERMINAL_NO - commandParamDTO.getTerminalNo().length();
        if (needFillZeroCount > 0) {
            for (int i = 0; i < needFillZeroCount; i++) {
                byteBuf.writeByte(0x00);
            }
        }
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 关闭自动调平功能
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f36(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getTerminalNo())) {
            log.error("f36 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(20);
        // DT=> F36
        byteBuf.writeShort(0x0804);
        // SN
        byteBuf.writeShort(sn);
        // terminalNo
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getTerminalNo())));
        // 不够16位，填充 0x00
        int needFillZeroCount = Constant.BYTE_LENGTH_TERMINAL_NO - commandParamDTO.getTerminalNo().length();
        if (needFillZeroCount > 0) {
            for (int i = 0; i < needFillZeroCount; i++) {
                byteBuf.writeByte(0x00);
            }
        }
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 调节周期设定
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f37(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getTerminalNo()) || ComUtil.isEmpty(commandParamDTO.getAdjustCycle())) {
            log.error("f37 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(22);
        // DT=> F37
        byteBuf.writeShort(0x1004);
        // SN
        byteBuf.writeShort(sn);
        // terminalNo
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getTerminalNo())));
        // 不够16位，填充 0x00
        int needFillZeroCount = Constant.BYTE_LENGTH_TERMINAL_NO - commandParamDTO.getTerminalNo().length();
        if (needFillZeroCount > 0) {
            for (int i = 0; i < needFillZeroCount; i++) {
                byteBuf.writeByte(0x00);
            }
        }
        // 调节周期
        byteBuf.writeShortLE(commandParamDTO.getAdjustCycle());
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 采暖季设置
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f38(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getHeatSeasonStart()) || ComUtil.isEmpty(commandParamDTO.getHeatSeasonEnd())) {
            log.error("f38 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(8);
        // DT=> F38
        byteBuf.writeShort(0x2004);
        // SN
        byteBuf.writeShort(sn);
        // 采暖季开始
        String heatSeasonStart = commandParamDTO.getHeatSeasonStart().replace("-", "");
        byteBuf.writeByte(CodeUtil.hexStringToByte(heatSeasonStart.substring(2, 4), 0));
        byteBuf.writeByte(CodeUtil.hexStringToByte(heatSeasonStart.substring(0, 2), 0));
        // 采暖季结束
        String heatSeasonEnd = commandParamDTO.getHeatSeasonEnd().replace("-", "");
        byteBuf.writeByte(CodeUtil.hexStringToByte(heatSeasonEnd.substring(2, 4), 0));
        byteBuf.writeByte(CodeUtil.hexStringToByte(heatSeasonEnd.substring(0, 2), 0));
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 设置清洗周期
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f39(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getCleanCycle())) {
            log.error("f39 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(6);
        // DT=> F39
        byteBuf.writeShort(0x4004);
        // SN
        byteBuf.writeShort(sn);
        // 清洗周期
        byteBuf.writeShortLE(commandParamDTO.getCleanCycle());
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 命令包：默认
     *
     * @param deviceType
     * @return
     */
    private static ByteBuf packDefault(int deviceType) {
        ByteBuf byteBuf = Unpooled.buffer(10);
        // 控制域
        byteBuf.writeByte(0x00);
        // 设备类型
        byteBuf.writeByte(deviceType);
        // 协议版本
        byteBuf.writeShort(0x0100);
        // 主站地址和组地址
        byteBuf.writeShort(0x0000);
        // AFN=13：阀门控制
        byteBuf.writeByte(0x13);
        // 帧序列域SEQ(保留0xff)
        byteBuf.writeByte(0xFF);
        // 信息点DA(暂时使用0x00,0x00)
        byteBuf.writeShort(0x0000);
        return byteBuf;
    }
}
